#!/usr/bin/env python

import sys
import qrtools
import os



def decode(path):
    qr = qrtools.QR()
    res = qr.decode(path)

    if res:
        print qr.data
    else:
        print "Error"  


def main(path):
    if path.startswith("http://") or path.startswith("https://"):
        import requests
        r = requests.get(path)
        tmppath = '/tmp/qr1x2x3'
        with open(tmppath, 'wb') as f:
            f.write(r.content)
        decode(tmppath)
        os.remove(tmppath)
    else:
       decode(path)


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print "Usage: %s path|url" % sys.argv[0]
        sys.exit(0)

    main(sys.argv[1])

